package com.simple.sso.client.utils;

import org.apache.http.Consts;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpUtils {

    /**
     * 向指定url发送post请求
     *
     * @param url      http请求
     * @param paramMap 参数键值对映射
     * @return 请求成功，正常响应返回true
     */
    public static boolean post(String url, Map<String, String> paramMap) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> nameValuePairs = new ArrayList<>();
        if (paramMap != null && !paramMap.isEmpty()) {
            for (Map.Entry<String, String> entry : paramMap.entrySet()) {
                String name = entry.getKey();
                String value = entry.getValue();
                BasicNameValuePair basicNameValuePair = new BasicNameValuePair(name, value);
                nameValuePairs.add(basicNameValuePair);
            }

            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, Consts.UTF_8));
        }

        try {
            CloseableHttpResponse response = httpClient.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            InputStream inputStream = response.getEntity().getContent();
            int len = 0;
            byte[] buff = new byte[1024];
            System.out.println("-->>响应内容：");
            while (-1 != (len = (inputStream.read(buff)))) {
                System.out.println(new String(buff, 0, len));
            }
            System.out.println("HttpUtils post method statusCode=" + statusCode);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return true;
    }

    public static boolean checkGlobleSessionValid(String url, Map<String, String> paramMap) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> nameValuePairs = new ArrayList<>();
        if (paramMap != null && !paramMap.isEmpty()) {
            for (Map.Entry<String, String> entry : paramMap.entrySet()) {
                String name = entry.getKey();
                String value = entry.getValue();
                BasicNameValuePair basicNameValuePair = new BasicNameValuePair(name, value);
                nameValuePairs.add(basicNameValuePair);
            }

            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, Consts.UTF_8));
        }

        try {
            CloseableHttpResponse response = httpClient.execute(httpPost);
            InputStream inputStream = response.getEntity().getContent();
            int len = 0;
            byte[] buff = new byte[1024];
            System.out.println("-->>响应内容：");
            while (-1 != (len = (inputStream.read(buff)))) {
                String result = new String(buff, 0, len);
                System.out.println(result);

                if ("true".equals(result)) {
                    return true;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }

    public static void main(String[] args) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("token", "0d83b2fb-3a9e-4333-97a6-917fc4a928bc");
        System.out.println(checkGlobleSessionValid("http://localhost:8080/server/checkGlobleSessionValid", paramMap));
    }
}
